package org.scut.smartgeometry.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import org.scut.smartgeometry.R;
import org.scut.smartgeometry.act.HomePageAct;
import org.scut.smartgeometry.dialog.BacPickerDialog;
import org.scut.smartgeometry.dialog.ColorPickerDialog;
import org.scut.smartgeometry.dialog.OpenDialog;
import org.scut.smartgeometry.dialog.SaveDialog;
import org.scut.smartgeometry.dialog.StylePickerDialog;
import org.scut.smartgeometry.utils.FileUtils;
import org.scut.smartgeometry.view.identify_system.Line_Unit;
import org.scut.smartgeometry.view.identify_system.SGPath;
import org.scut.smartgeometry.view.identify_system.SGPoint;
import org.scut.smartgeometry.view.identify_system.ShapeIdentifySystem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 13-6-5.
 */
public class HandwriteView extends View{

    private HomePageAct homePageAct;
    private List<SGPath> paths;
    private List<SGPath> removePaths;
    private Paint paint = null;
    private SGPath path = null;
    private Bitmap bitmap = null;

//    private Line_Unit line_unit;

    private List<SGPoint> points;

    private ShapeIdentifySystem shapeIdentifySystem;

    public HandwriteView(Context context, HomePageAct homePageAct) {
        super(context);
        this.homePageAct = homePageAct;

        paths = new ArrayList<SGPath>();
        removePaths = new ArrayList<SGPath>();
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6);

        shapeIdentifySystem = ShapeIdentifySystem.getSingleton();

//        points = new ArrayList<SGPoint>();

//        line_unit = new Line_Unit();

        this.setBackgroundResource(R.xml.shape_paper);
    }




    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if(event.getAction() == MotionEvent.ACTION_DOWN){
            path = new SGPath(paint.getColor(), paint.getStyle(), paint.getStrokeWidth());
            path.moveTo(x, y);
            invalidate();
            paths.add(path);

            points = new ArrayList<SGPoint>();
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_MOVE){
            path.lineTo(x, y);
            SGPoint point =new SGPoint(x,y);

            if(!points.isEmpty()){
                SGPoint lastPoint = points.get(points.size()-1);
                float dis = (float) Math.sqrt(Math.pow(point.getX() - lastPoint.getX(),2) +
                        Math.pow(point.getY() - lastPoint.getY(),2));
                if(dis > 20){
                    points.add(point);
//                    Log.i("hbb", "dis = " + dis + "           xy == " + x +"    "+ y);
                }
            }else {
                points.add(point);
            }
            invalidate();
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_UP){

            /*line_unit.setPath(path);
            line_unit.setPoints(points);
            if(line_unit.smartG()){
                line_unit.Linepaint();
            }*/

            if(points.size() != 0){
                shapeIdentifySystem.shapeIdentify(points, path);
            }
            else{
                paths.remove(path);
            }

            invalidate();

            return true;
        }
        return super.onTouchEvent(event);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        if(path != null){
//            canvas.drawPath(path, paint);
//        }
        int nColor = paint.getColor();
        Paint.Style nStyle = paint.getStyle();
        float nStrokeWidth = paint.getStrokeWidth();

        for (int i = 0; i < paths.size(); i++){
            SGPath sgPath = paths.get(i);
            paint.setColor(sgPath.getColor());
            paint.setStyle(sgPath.getStyle());
            paint.setStrokeWidth(sgPath.getStrokeWidth());

            canvas.drawPath(sgPath, paint);
        }

        paint.setColor(nColor);
        paint.setStyle(nStyle);
        paint.setStrokeWidth(nStrokeWidth);

        canvas.save(Canvas.CLIP_SAVE_FLAG);
    }


    public void clickMenu(String btnName){
        Resources res = getResources();
        if(btnName.equalsIgnoreCase(res.getString(R.string.redo))){
            redo();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.undo))){
            undo();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.style))){
            style();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.clear))){
            clear();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.open))){
            open();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.save))){
            save();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.color))){
            color();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.background))){
            background();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.chart))){
            chart();
        }else if(btnName.equalsIgnoreCase(res.getString(R.string.exit))){
            exit();
        }
    }
    private void redo(){
        int size = removePaths.size();
        if(size == 0) return;
        SGPath sgPath = removePaths.get(size - 1);
        removePaths.remove(sgPath);
        paths.add(sgPath);
        invalidate();
    }
    private void undo(){
        int size = paths.size();
        if(size == 0) return;
        SGPath sgPath = paths.get(size - 1);
        paths.remove(sgPath);
        removePaths.add(sgPath);
        invalidate();
    }

    private void style(){
        StylePickerDialog stylePicker = new StylePickerDialog(getContext(), new StylePickerDialog.OnStyleChangedListener() {
            @Override
            public void styleChanged(Paint.Style style) {
                paint.setStyle(style);
            }
        });

        stylePicker.show();
    }

    private void open(){
        OpenDialog openDialog = new OpenDialog(getContext(), new OpenDialog.OpenCallBack() {
            @Override
            public void openForResult(Bitmap bitmap) {
                setBackground(new BitmapDrawable(getResources(), bitmap));
            }
        });
        openDialog.show();
    }

    private void save(){
        SaveDialog saveDialog = new SaveDialog(getContext(), new SaveDialog.SaveCallBack() {
            @Override
            public void save(String fileName) {
                Bitmap bitmap = Bitmap.createBitmap(
                        getWidth(),
                        getHeight(),
                        Bitmap.Config.ARGB_8888);


                Canvas canvas = new Canvas();
                canvas.setBitmap(bitmap);
                draw(canvas);

                Boolean result = FileUtils.saveBitmap(fileName, bitmap);

                Log.i("hb", "save bitmap result  == " + result + "   filename == " + fileName);
            }
        });
        saveDialog.show();

    }

    private void color(){

        ColorPickerDialog colorPicker = new ColorPickerDialog(getContext(), new ColorPickerDialog.OnColorChangedListener() {
            @Override
            public void colorChanged(int color) {
                paint.setColor(color);
            }
        }, Color.BLACK);
        colorPicker.show();
    }

    private void background(){

        BacPickerDialog bacPicker = new BacPickerDialog(getContext(), new ColorPickerDialog.OnColorChangedListener() {
            @Override
            public void colorChanged(int color) {
                setBackgroundColor(color);
            }
        }, Color.WHITE, homePageAct);

        bacPicker.show();
    }

    private void chart(){

    }

    public void clear(){

        bitmap = null;
        paths.clear();
        removePaths.clear();
        invalidate();

        shapeIdentifySystem.clear();
    }


    private void exit(){
        homePageAct.finish();
    }


    public void selectBacForResult(Bitmap bitmap){
        BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);
        setBackgroundDrawable(drawable);
    }


}
